<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
      >{{ isEdit ? '完成': '编辑' }}</van-button>
    </van-cell>
<van-grid :gutter="10">
    <van-grid-item
                   class="channel-item"
                   v-for="(item, index) in channels"
                   :key="index"
                   @click="onMyChannelClick(item, index)"
                   >
                   <span class="text" :class="{active: active === index}" slot="text">{{ item.name }}</span>
                   <van-icon v-show="isEdit && index!==0" slot="icon" name="close" />
    </van-grid-item>
</van-grid>
    <van-cell title="频道推荐" :border="false"></van-cell>
<van-grid class="recommend-grid" :gutter="10">
        <van-grid-item
          class="channel-item"
          v-for="item in recommendChannels"
          :key="item.id"
          :text="item.name"
          icon="plus"
          @click="addChannels(item)"
        />
      </van-grid>
  </div>
</template>

<script>
import { getAllChannels } from '@/api/home'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false
    }
  },
  computed: {
    recommendChannels () {
      return this.allChannels.filter(channel => {
        const mychannel = this.channels.find(myChannel => {
          return myChannel.id === channel.id
        })
        return !mychannel
      })
    }
  },
  watch: {},
  created () {
    this.getAllChannels()
  },
  mounted () {},
  methods: {
    async getAllChannels () {
      const res = await getAllChannels()
      this.allChannels = res.data.data.channels
      console.log(this.allChannels)
    },
    addChannels (item) {
    //   this.channels.push(item)
      this.$emit('addChannels', item)
    },
    onMyChannelClick (item, index) {
      if (this.isEdit) {
        // 执行删除操作
        if (index === 0) return
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        // this.channels.splice(index, 1)
        this.$emit('removeChannels', index)
      } else {
        // 执行跳转操作
        this.$emit('update-active', index)
      }
    }
  }
}
</script>

<style scoped lang="less">
/deep/ .recommend-grid {
    .van-grid-item__content {
        flex-direction: row;
        color: #222;
        .van-icon {
            font-size: 24px;
        }

        .van-grid-item__text {
            font-size: 28px;
            margin-top: 0;
        }
    }
}
/deep/ .van-icon-close {
    position: absolute;
    right: -10px;
    top: -10px;
    font-size: 36px;
    color: #ccc;
}
/deep/ .van-grid-item__content {
      background-color: #f5f5f6;
      .van-grid-item__text, .text {
        color: #222;
        font-size: 28px;
        white-space: nowrap;
      }

      .active {
        color: red;
      }
    }
 /deep/ .van-grid-item__icon-wrapper {
        position: unset;
      }
</style>
